package com.bdzt.beidoubox.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.bdzt.beidoubox.R;
import com.bdzt.beidoubox.adapter.MessageAdapter;
import com.bdzt.beidoubox.entity.MessageBean;
import com.bdzt.beidoubox.utils.OtherUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 、默 on 3/28/2017.
 */

public class MessageFragment extends Fragment {
    private ListView lv_message;
    private Context mContext;
    private List<MessageBean> list;
    private MessageAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_message, container, false);

        initView(view);
        initData();
        return view;
    }

    private void initData() {
        for (int i = 1; i <= 10; i++) {
            MessageBean message = new MessageBean();
            message.setContext("我也是一个很帅很帅很帅很帅很帅很帅很帅很帅很帅很帅很帅的内容" + i);
            message.setTitle("我是一个很长很长很长很长很长的标题" + i);
            message.setDate(OtherUtils.getDateTime());

            list.add(message);
        }
        adapter.notifyDataSetChanged();

    }

    private void initView(View view) {
        mContext = getActivity();
        list = new ArrayList<>();
        lv_message = (ListView) view.findViewById(R.id.lv_message);
        adapter = new MessageAdapter(list,mContext);
        lv_message.setAdapter(adapter);

        view.findViewById(R.id.iv_tongBu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.clear();
                adapter.notifyDataSetChanged();
                new AlertDialog.Builder(mContext)
                        .setTitle("提示")
                        .setMessage("信息同步中，请稍后...")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        }).show();
            }
        });
    }
}
